//
//  ViewController.swift
//  SwiftTTPageController
//
//  Created by gener on 2018/8/8.
//  Copyright © 2018年 Light. All rights reserved.
//

import UIKit

class ViewController: UIViewController,TTHeadViewDelegate,TTPageViewControllerDelegate ,MXYPPageMenuDelegate,MXYPPageVCDelegate {
    
    @IBOutlet weak var pageMenuView: UIView!
    @IBOutlet weak var vcContainView: UIView!
    
    // 其他
    var otherHeadView:TTHeadView!
    var pagevc:TTPageViewController!
    
    // 优品效果
    var ypHeadView:MXYPPageMenu!
    var ypPagevc:MXYPPageVC!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = "外卖订单"

        changeNavBg()
        setupYpPageMenu()
    }
    
    func changeNavBg() {
        self.navigationController?.navigationBar.barTintColor = UIColor(red: 0.19, green: 0.23, blue: 0.34, alpha: 1)
        //修改导航栏文字颜色
        self.navigationController?.navigationBar.titleTextAttributes =
            [NSAttributedString.Key.foregroundColor: UIColor.white]
        // 去掉下边缘线
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
    }
    
    //MARK: - 其他的效果
    func setupOtherMenu() {
        let titles = ["新订单","待抢单","配送中","已完成","售后/退款"]
        var attri = TTHeadTextAttribute()
        attri.needBottomLine = true
        attri.bottomLineHeight = 0
        attri.itemWidth = UIScreen.main.bounds.width/CGFloat(titles.count)
        //创建headView
        
        otherHeadView = TTHeadView (frame: CGRect (x: 0, y: 0, width: UIScreen.main.bounds.width, height: self.pageMenuView.frame.size.height), titles: titles, delegate: self ,textAttributes:attri)
        
        self.pageMenuView.addSubview(otherHeadView);
        
        //创建TTPageViewController
        let vcs = [TableViewController(),TableViewController(),TableViewController(),TableViewController(),TableViewController(),TableViewController(),TableViewController(),TableViewController(),]
        
        let frame = CGRect (x: 0, y: 0, width: view.frame.width, height: view.frame.height)
        pagevc = TTPageViewController.init(controllers: vcs, frame: frame, delegate: self)
        
        //添加到当前控制器视图
        self.addChild(pagevc)
        self.vcContainView.addSubview(pagevc.view)
    }
    
    func tt_headViewSelectedAt(_ index: Int) {
        pagevc.scrollToPageAtIndex(index)
    }
    
    func tt_pageControllerSelectedAt(_ index: Int) {
        otherHeadView.scrollToItemAtIndex(index)
    }
    
    //MARK: - 优品的效果
    func setupYpPageMenu() {
        let titles = ["新订单","待抢单","配送中","已完成","售后/退款"]
        var titleModes = [MXYPPageMenuMode]()
        for str in titles {
            var m = MXYPPageMenuMode()
            m.title = str
            m.isSelect = false
            titleModes.append(m)
        }
        
        var attri = MXYPPageMenuAttribute()
        attri.needBottomLine = true
        attri.bottomLineHeight = 0
        attri.itemWidth = UIScreen.main.bounds.width/CGFloat(titles.count)
        //创建headView
        
        ypHeadView = MXYPPageMenu (frame: CGRect (x: 0, y: 0, width: UIScreen.main.bounds.width, height: self.pageMenuView.frame.size.height), titles: titleModes, delegate: self ,textAttributes:attri)
        
        self.pageMenuView.addSubview(ypHeadView);
        
        //创建TTPageViewController
        let vcs = [TableViewController(),
                   TableViewController(),
                   TableViewController(),
                   TableViewController(),
                   TableViewController(),]
        
        let frame = CGRect (x: 0, y: 0, width: view.frame.width, height: view.frame.height)
        ypPagevc = MXYPPageVC.init(controllers: vcs, frame: frame, delegate: self)
        
        //添加到当前控制器视图
        self.addChild(ypPagevc)
        self.vcContainView.addSubview(ypPagevc.view)
    }
    
    func yp_headViewSelectedAt(_ index: Int) {
        ypPagevc.scrollToPageAtIndex(index)
    }
    
    func yp_pageControllerSelectedAt(_ index: Int) {
        ypHeadView.scrollToItemAtIndex(index)
    }
    
}

